package com.gexiaobao.pigeon.ui.fragment.mall

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentAfterSalesBinding
import com.gexiaobao.pigeon.ui.adapter.AfterSalesAdapter
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/7/6 15:38
 * @Describe    : 售后
 */
class FragmentAfterSales : BaseFragment<MallViewModel, FragmentAfterSalesBinding>() {

    private val mAdapter: AfterSalesAdapter by lazy { AfterSalesAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = getString(R.string.Return_after_sales)

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 10f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.afterSales(false)
            }
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(true)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_return_detail, Bundle().apply {
                putInt("orderId", mAdapter.data[position].orderId)
                putInt("shopId", mAdapter.data[position].shopId)
                putString("goodsId", mAdapter.data[position].goodId.toString())
            })
        }

        mAdapter.addChildClickViewIds(R.id.tvItemAfterSales, R.id.llTop)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvItemAfterSales -> {
                    when (mAdapter.data[position].orderStatus) {
                        11, 13, 14 -> {
                            nav().navigateAction(R.id.action_to_fragment_return_detail, Bundle().apply {
                                putInt("orderId", mAdapter.data[position].orderId)
                                putInt("shopId", mAdapter.data[position].shopId)
                                putString("goodsId", mAdapter.data[position].goodId.toString())
                            })
                        }

                        15 -> {//退货审核通过
                            nav().navigateAction(R.id.action_to_fragment_send_goods_out, Bundle().apply {
                                putInt("shopId", mAdapter.data[position].shopId)
                                putInt("orderId", mAdapter.data[position].shopOrderId)
                                putInt("childOrderId", mAdapter.data[position].orderId)
                            })
                        }
                    }
                }

                R.id.llTop -> {
                    nav().navigateAction(R.id.action_to_fragment_merchant_store, Bundle().apply {
                        putInt("shopId", mAdapter.data[position].shopId)
                    })
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        showLoading(getString(R.string.loading))
        onRefresh(true)
    }

    private fun onRefresh(isRefresh: Boolean) {
        showLoadingAntiShake(getString(R.string.loading))
        mViewModel.afterSales(isRefresh)
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取我的订单列表 */
        mViewModel.afterSalesResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            nav().navigateUp()
        }
    }
}